package ua.kpi_java_training.zabrodskyi.module3;

import static org.junit.Assert.*;

import org.junit.Test;

/**
 * @author Maksym Zabrodskyi
 * @version 23-Jun-2013
 */
public class PredatorTest {

	Predator predator;
	Cell cell;

	@Test
	public void shouldCreatePredator() {
		cell = new Cell(1, 2);
		predator = new Predator(cell);
		assertNotNull(predator);
	}

	@Test(expected = Exception.class)
	public void shouldExceptionCreatePredator() {
		cell = new Cell(-1, -2);
		predator = new Predator(cell);
	}

	@Test
	public void shouldMovePredator() {
		Environment environment = new Environment(Settings.getWidth(),
				Settings.getHeight());
		environment.add(ElementType.PREDATOR);
		predator = (Predator) environment.getElement(0);
		cell = predator.getCell();
		predator.move(environment);
		Cell newCell = predator.getCell();
		assertFalse(cell.equals(newCell));
	}

	@Test
	public void shouldPredatorEatVictim() {
		Environment environment = new Environment(Settings.getWidth(),
				Settings.getHeight());
		environment.add(ElementType.PREDATOR);
		environment.add(ElementType.VICTIM);
		environment.add(ElementType.VICTIM);
		predator = (Predator) environment.getElement(0);
		cell = new Cell(2, 1);
		predator.setCell(cell);
		Victim victim = (Victim) environment.getElement(1);
		cell = new Cell(3, 3);
		victim.setCell(cell);
		victim = (Victim) environment.getElement(2);
		cell = new Cell(4, 2);
		victim.setCell(cell);
		for (int time = 0; time < 2; time++) {
			predator.move(environment);
			predator.eat(environment);
		}
		int actualCountElements = environment.getCountAllElements();
		assertEquals(1, actualCountElements);
	}

	@Test
	public void shouldGetCellInWitchPredatorIsIt() {
		cell = new Cell(1, 4);
		predator = new Predator(cell);
		Cell newCell = predator.getCell();
		assertTrue(cell.equals(newCell));
	}

	@Test
	public void shouldSetNewCellForPredator() {
		cell = new Cell(3, 5);
		predator = new Predator(cell);
		Cell newCell = new Cell(1, 1);
		predator.setCell(newCell);
		cell = predator.getCell();
		assertTrue(cell.equals(newCell));
	}

	@Test(expected = Exception.class)
	public void shouldExceptionSetNewCellForPredator() {
		cell = new Cell(1, 2);
		predator = new Predator(cell);
		Cell newCell = new Cell(-1, -2);
		predator.setCell(newCell);
	}

	@Test
	public void shouldGetTypePredator() {
		cell = new Cell(2, 5);
		predator = new Predator(cell);
		ElementType actualType = predator.getType();
		assertEquals(ElementType.PREDATOR, actualType);
	}

	@Test
	public void testToString() {
		cell = new Cell(4, 5);
		predator = new Predator(cell);
		String expected = "PREDATOR is on cell [4,5]";
		assertEquals(expected, predator.toString());
	}

}
